﻿using log4net;
using Mathmall.Administration.common;
using Mathmall.Administration.Service;
using Mathmall.Administration.Vo;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using Mathmall.Administration.Models;
using Microsoft.AspNetCore.Authorization;

namespace Mathmall.Administration.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CategoryController : ControllerBase
    {
        private static readonly ILog _logger = LogManager.GetLogger(typeof(CategoryController));

        private readonly ICategoryService _categoryService;

        public CategoryController(ICategoryService categoryService)
        {
            _categoryService = categoryService;
        }

        /// <summary>
        /// 添加商品分类
        /// </summary>
        /// <param name="categoryVo"></param>
        /// <returns></returns>
        [Authorize(Policy = "AddCategory")]
        [HttpPost]
        public async Task<ActionResult<int>> AddCategory(CategoryVo categoryVo)
        {
            await _categoryService.AddCategory(categoryVo);
            return CreatedAtAction("AddCategory", null);
        }

        /// <summary>
        /// 查询分类信息，以树形结构返回
        /// <param name="parentCategoryId">父分类id</param>
        /// </summary>
        [Authorize(Policy = "QueryCategory")]
        [HttpGet("{parentCategoryId}")]
        public ActionResult<List<CategoryVo>> QueryCategoryVosByParentId(int? parentCategoryId)
        {
            return _categoryService.QueryCategoryVosByParentId(parentCategoryId);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        [Authorize(Policy = "QueryCategory")]
        [HttpGet("{page}/{pageSize}")]
        public async Task<ActionResult<PaginatedList<CategoryVo>>> PaginatedQuery(int page, int pageSize)
        {
            PaginatedList<CategoryVo>? categoryVos = await _categoryService.PaginatedQuery(page, pageSize);

            if (categoryVos != null)
            {
                string v = JsonSerializer.Serialize(new R<PaginatedList<CategoryVo>>().Success(categoryVos),
                    new JsonSerializerOptions()
                    {
                        Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
                    });
                return categoryVos;
            }
            else
            {
                return NotFound();
            }
        }

        /// <summary>
        /// 递归查询全部分类，以树形结构返回
        /// </summary>
        /// <returns></returns>
        [Authorize(Policy = "QueryCategory")]
        [HttpGet]
        public async Task<List<CategoryVo>> QueryAllCategories()
        {
            return await _categoryService.QueryAllCategories();
        }

        /// <summary>
        /// 更新分类信息
        /// </summary>
        /// <param name="categoryVo"></param>
        /// <returns></returns>
        [Authorize(Policy = "EditCategory")]
        [HttpPut]
        public async Task<int> UpdateCategory(CategoryVo categoryVo)
        {
            return await _categoryService.UpdateCategory(categoryVo);
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="categoryIds"></param>
        /// <returns></returns>
        [Authorize(Policy = "DeleteCategory")]
        [HttpDelete]
        public async Task<ActionResult<int>> DeleteCategories(List<int> categoryIds)
        {
            return await _categoryService.DeleteCategories(categoryIds);
        }
        
            
        /// <summary>
        /// 用循环返回树形结构
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        public async Task<ActionResult<List<CategoryVo>>> GetCategories()
        {
            return await _categoryService.GetAllCategoryVos();
        }
    }
}